package com.zhanggm.springcloud.repair.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhanggm.springcloud.common.entity.Car;
import com.zhanggm.springcloud.common.entity.CarRepairRecord;
import com.zhanggm.springcloud.common.entity.CarRepairShop;
import com.zhanggm.springcloud.common.entity.Person;
import com.zhanggm.springcloud.common.model.RepairModel;
import com.zhanggm.springcloud.repair.api.CarApi;
import com.zhanggm.springcloud.repair.mapper.CarRepairRecordMapper;
import com.zhanggm.springcloud.repair.service.ICarRepairRecordService;
import com.zhanggm.springcloud.repair.service.IPersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 车辆维修记录表 服务实现类
 * </p>
 *
 * @author zhanggm
 * @since 2020-12-12
 */
@Service
public class CarRepairRecordServiceImpl extends ServiceImpl<CarRepairRecordMapper, CarRepairRecord> implements ICarRepairRecordService {

    @Autowired
    private IPersonService iPersonService;

    @Autowired
    private CarApi carApi;

    @Override
    public Page getPageInfo(RepairModel repairModel, Integer pageNum, Integer pageSize) {
        QueryWrapper queryWrapper = new QueryWrapper();
        //车辆id查询
        if(repairModel.getCarId()!=null){
            queryWrapper.eq("car_id",repairModel.getCarId());
        }
        //车牌号模糊查询
        if(repairModel.getCarName()!=null){
            List<Car> cars = carApi.listByName(repairModel.getCarName());
            if(cars!=null && cars.size()>0){
                List<Integer> carIdList = cars.stream().map(c -> c.getId()).collect(Collectors.toList());
                queryWrapper.in("car_id",carIdList);
            }else {
                return new Page();
            }
        }
        Page<CarRepairRecord> page = this.page(new Page<>(pageNum, pageSize), queryWrapper);
        //查询车辆信息
        List<Integer> carIds = page.getRecords().stream().map(r -> r.getCarId()).collect(Collectors.toList());
        /*List<Integer> carIdList = new ArrayList<>();
        page.getRecords().forEach(r->{
            carIdList.add(r.getCarId());
        });*/
        List<Car> carList = carApi.getListByIds(carIds);

        page.getRecords().forEach(r->{
            //根据维修人员id查询person对象，设置维修人员名字
            Integer personId = r.getPersonId();
            Person person = iPersonService.getById(personId);
            r.setPersonName(person.getName());
            //设置维修厂名称
            CarRepairShop shop = carApi.getShopById(r.getShopId());
            r.setShopName(shop.getName());
            //设置车辆
            /*Car car = carApi.getById(r.getCarId());*/
            carList.forEach(c->{
                if(c.getId().intValue() == r.getCarId().intValue()){
                    r.setCarName(c.getName());
                }
            });

        });
        return page;
    }
}
